const express = require('express')
const router = express.Router()

const multer = require('multer')
const path = require('path')

const adminControl = require('../controller/adminControl')

router.get('/', (req, res) => {
  res.send('hello world')
})
router.post('/signup', adminControl.putUser)
router.post('/userlogin', adminControl.userLogin)
router.get('/getServiceList', adminControl.getServiceList)
router.get('/getTeamList', adminControl.getTeamList)
router.post('/addTeamMember', adminControl.addTeamMember)

router.post('/addBanner', adminControl.addBanner)
router.get('/getBannerList', adminControl.getAllBanner)
router.post('/updateBanner', adminControl.updateBanner)

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, '../../static/uploads/'))
  },
  filename: function (req, file, cb) {
    try {
      let ext = ''
      let namearr = file.originalname.split('.')
      ext = namearr[namearr.length - 1]

      cb(null, file.fieldname + '-' + Date.now() + '.' + ext)
    } catch(e) {}
  }
})

const upload = multer({ storage: storage }) // 文件储存路径
router.post('/singleupload', upload.single('uploadimg'), function(req, res, next) {
  // console.log(path.join(__dirname, '../../static/uploads'))
  let file = req.file;
  // console.log(file)
  res.json({
    code: 200,
    detail: {
      path: '/uploads/' + file.filename
    }
  });
});


module.exports = router
